///<reference path="../typings/tsd.d.ts" />

interface JQuery {
    submenupicker(): JQuery;
    tabs(a?: any, b?: any): JQuery;
    menu(a?: any, b?: any): JQuery;
}
interface Window {
    addFrameTab: (title: string, url: string) => void;
    $: JQueryStatic;
}
$(function () {
    //系统菜单
    $.ajax("/api/main/getMainmenu", { data: { name: "xtgly1" }, type: "POST", dataType: 'json' })
        .done(function (menuData) {
            var setting = {
                data: {
                    key: { name: "NAME", title: "NAME" },
                    simpleData: { enable: true, idKey: "ID", pIdKey: "PID" }
                },
                callback: {
                    onClick: function (event, treeId, treeNode: TreeNode) {
                        //如果是叶子节点，则打开新标签
                        treeNode.ISLEAF && window.addFrameTab(treeNode.NAME, treeNode.URL);
                    }
                }
            };
            var treeObj = $.fn.zTree.init($("#ulOrgTree"), setting, menuData);
            var nodes = treeObj.getNodes();
            treeObj.expandNode(nodes[0], true, false, true);
        });
    /**设置标签功能 */
    $('#topTabs').tabs({
        fit: true,
        border: false
    });
    //添加标签  
    window.addFrameTab = function (title, url) {
        if ($('#topTabs').tabs('exists', title)) {
            //已经存在，则更新一下url
            let tab = $('#topTabs').tabs('getTab', title);
            tab.find("iframe").attr("src", url);
            $('#topTabs').tabs('select', title);
        } else {
            var _content = $('<iframe allowfullscreen scrolling="auto" ></iframe>');
            $('#topTabs').tabs('add', { title: title, content: _content[0], url: url, closable: true, closed: true, fit: true });
            _content.attr('id', 'tab_' + title).attr('title', title).attr('src', url).attr('frameborder', '0').css({ width: "100%", height: "100%" })
        }
    }

    //右键,双击
    $('#topTabs').on('contextmenu', '.tabs-inner', function (e) {
        $('#mm').menu('show', { left: e.pageX, top: e.pageY });
        $('#mm').data("currtab", $(this).children("span").text());
        return false;
    }).on('dblclick', '.tabs-inner', function () {
        $('#topTabs').tabs('close', $(this).children(".tabs-title").text());
    });
    //关闭当前
    $('#mm-tabclose').click(function () {
        $('#topTabs').tabs('close', $('#mm').data("currtab"));
    });
    //全部关闭
    $('#mm-tabcloseall').click(function () {
        $('.tabs-inner span').each(function (i, n) {
            if ($(n).hasClass('tabs-closable') == false) return;
            $('#topTabs').tabs('close', $(n).text());
        });
    });
    //关闭其它
    $('#mm-tabcloseother').click(function () {
        $('.tabs-inner span').each(function (i, n) {
            if ($(n).hasClass('tabs-closable') == false) return;
            if ($(n).text() == $('#mm').data("currtab")) return;
            $('#topTabs').tabs('close', $(n).text());
        });
    });
    //关闭右侧
    $('#mm-tabcloseright').click(function () {
        var count = 0, nextall = $('.tabs-selected').nextAll();
        nextall.each(function (i, n) {
            if ($('.tabs-title', $(n)).hasClass('tabs-closable') == false) return;
            $('#topTabs').tabs('close', $('a:eq(0) span', $(n)).text()); count = count + 1;
        });
        return false;
    });
    //关闭左侧
    $('#mm-tabcloseleft').click(function () {
        var count = 0, prevall = $('.tabs-selected').prevAll();
        prevall.each(function (i, n) {
            if ($('.tabs-title', $(n)).hasClass('tabs-closable') == false) return;
            $('#topTabs').tabs('close', $('a:eq(0) span', $(n)).text()); count = count + 1;
        });
        return false;
    });

    //主动更新某个标签页的机构
    //window.updateTabZZJG = function(title:string) {
    //    var $tab = $('#topTabs').tabs('getTab', title);
    //    if ($tab.length == 1)
    //        (<HTMLIFrameElement>$tab.find("iframe")[0]).contentWindow.postMessage(JSON.stringify(window.CurTreeNode), "*");
    //}

});